﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Boiling.DataAccess
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;
    using System.Text;

    using Boiling.Common;

    public class b_Diary : IDisposable
    {
        private SqlDataAdapter dscommand;

        public b_Diary()
        {
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
                return;
            if (dscommand != null)
            {
                if (dscommand.SelectCommand != null)
                {
                    if (dscommand.SelectCommand.Connection != null)
                        dscommand.SelectCommand.Connection.Dispose();
                    dscommand.SelectCommand.Dispose();
                }
                dscommand.Dispose();
                dscommand = null;
            }
        }
        //
        //指令资源分配方式的代码段，与逻辑无关
        //


        public b_Diarydata getdiarybyNO(int diaryid)
        {
            //用日志标号返回日志，唯一
            b_Diarydata newdata = new b_Diarydata();
            string selectstring = "SELECT * FROM b_Diary WHERE Diary_ID = " + diaryid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Diary");
            tempconn.Close();
            return (newdata);
        }

        public b_Diarydata getdiarybyID(string username)
        {
            //用用户注册邮箱返回日志列表
            b_Diarydata newdata = new b_Diarydata();
            string selectstring = "SELECT * FROM b_Diary WHERE User_ID = '" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Diary");
            tempconn.Close();
            return (newdata);
        }

        public int countDiarybyID(string username)
        {
            //计算某人一共有多少篇日志，要求输入其邮箱
            b_Diarydata newdata = getdiarybyID(username);
            return (newdata.Tables["b_Diary"].Rows.Count);
        }


        public bool deleteDiarybyno(int diaryid)
        {
            //根据日志标号删除一篇日志
            b_Diarydata newdata = new b_Diarydata();
            string selectstring = "SELECT * FROM b_Diary WHERE Diary_ID =" + diaryid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Diary");

            if (newdata.Tables["b_Diary"].Rows.Count == 0)
                return (false);
            else
            {
                newdata.Tables["b_Diary"].Rows[0].Delete();
                dscommand.Update(newdata, "b_Diary");
                tempconn.Close();
                return (true);
            }
        }

        public bool deleteDiarybyID(string username)
        {
            //根据用户注册邮箱删除所有由其发表的日志
            b_Diarydata newdata = new b_Diarydata();
            string selectstring = "SELECT * FROM b_Diary WHERE User_ID ='" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Diary");

            if (newdata.Tables["b_Diary"].Rows.Count == 0)
                return (false);
            else
            {
                for (int i = newdata.Tables["b_Diary"].Rows.Count; i > 0; i--)
                {
                    newdata.Tables["b_Diary"].Rows[i - 1].Delete();
                }
                dscommand.Update(newdata, "b_Diary");
                tempconn.Close();
                return (true);
            }
        }

        public bool modifydiary(b_Diarydata tempdata)
        {
            //输入一个日志数据类型，修改ID相等的数据类型
            //若找不到ID相同的日志，则返回false,否则返回true
            int diaryid = Int32.Parse(tempdata.Tables["b_Diary"].Rows[0]["Diary_ID"].ToString());
            b_Diarydata newdata = new b_Diarydata();
            string selectstring = "SELECT * FROM b_Diary WHERE Diary_ID =" + diaryid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Diary");
            if (newdata.Tables["b_Diary"].Rows.Count == 0)
                return (false);
            else
            {
                for (int i = 0; i < tempdata.Tables["b_Diary"].Columns.Count; i++)
                    newdata.Tables["b_Diary"].Rows[0][i] = tempdata.Tables["b_Diary"].Rows[0][i];
                dscommand.Update(newdata, "b_Diary");
                tempconn.Close();
                return (true);
            }
        }
        public bool insertdiary(b_Diarydata tempdata)
        {
            //输入一个日志数据类型，向数据库中插入此数据类型
            string userid = tempdata.Tables["b_Diary"].Rows[0][1].ToString();
            string diarytitle = tempdata.Tables["b_Diary"].Rows[0][2].ToString();
            string diarycontent = tempdata.Tables["b_Diary"].Rows[0][3].ToString();
            string diarydate = tempdata.Tables["b_Diary"].Rows[0][4].ToString();
            string insertstring = "INSERT b_Diary (User_ID,Diary_Title,Diary_Content,Diary_Date) VALUES('" + userid + "','" + diarytitle + "','" + diarycontent + "','" + diarydate + "')";
            SqlConnection cn = new SqlConnection(BoilingConfiguration.dbConnnectionstring);
            SqlCommand cmd = new SqlCommand(insertstring, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            return (true);

        }



    }
}
